package com.zhh.service.impl;

import com.zhh.common.ResponseCode;
import com.zhh.common.ServerResponse;
import com.zhh.dao.FacilityDao;
import com.zhh.dto.FacilityDto;
import com.zhh.pojo.TbFacility;
import com.zhh.service.FacilityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FacilityServiceImpl implements FacilityService {

    @Autowired
    private FacilityDao facilityDao;

    @Override
    public ServerResponse insert(FacilityDto dto) {
        if (dto ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"参数错误");
        }
        TbFacility facility = dto2Facility(dto);
        TbFacility tbFacility =  facilityDao.saveAndFlush(facility);
        if (tbFacility !=null){

            return ServerResponse.createBySuccess(tbFacility);
        }else {
            return ServerResponse.createBySuccessMessage("录入设备失败");
        }
    }

    @Override
    public ServerResponse updateState(int id, int state) {
        return null;
    }


    private TbFacility dto2Facility(FacilityDto dto){
        if (dto==null){
            return null;
        }
        TbFacility facility = new TbFacility();
        facility.setItem(dto.getName());
        facility.setItemNo(dto.getUnittype());
        if (StringUtils.isBlank(dto.getType())){
//            facility.setType(dto.getType());
        }
        return facility;
    }
}
